﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BLL;
public partial class Fileupload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
          
            string q = Request.QueryString["q"].ToString();
        }

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string name = Session["admin"].ToString();
        string path = Server.UrlDecode(Request.QueryString["path"].ToString());
        string q = Request.QueryString["q"].ToString();
     
        if (FileUpload1.HasFile)
        {
            string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
            if (fileExt == ".asp" || fileExt == ".aspx" || fileExt == ".php" || fileExt == ".jsp")
            {
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('当前系统禁止上传的文件类型:asp,aspx,php,jsp!');</script>");
            }
            else
            {
                if (q == "aa")
                {    
                    string filename = System.IO.Path.GetFileName(FileUpload1.FileName);
                    string fileext = System.IO.Path.GetExtension(FileUpload1.FileName);
                    
                    int size = Convert .ToInt32 (FileUpload1.PostedFile.ContentLength.ToString());
                    
                    bool b = new FileInfoManager().isExists(name, 0, filename, fileext);
                    if (b)
                    {
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('文件已存在');</script>");
                    }
                    else
                    {

                        string filename2 = filename.Replace(fileext, "");
                        bool b1 = new FileInfoManager().Insert(name, 0, filename2.ToString(), fileext, size);
                        if (b1)
                        {
                            FileUpload1.SaveAs(Server.MapPath("Upfile") + "\\" + name + "\\" + FileUpload1.FileName);
                        }

                    }

                }
                else
                {
                   
                    int myparentid = Convert.ToInt32(Request.QueryString["p"].ToString());

                    string dirname = Server.UrlDecode(Request.QueryString["dd"].ToString());
                    int parentid = new DirInfoManager().FindId(name, myparentid, dirname);
                    string filename = System.IO.Path.GetFileName(FileUpload1.FileName);
                    string fileext = System.IO.Path.GetExtension(FileUpload1.FileName);

                    int size = Convert.ToInt32(FileUpload1.PostedFile.ContentLength.ToString());

                    bool b = new FileInfoManager().isExists(name, parentid, filename, fileext);
                    if (b)
                    {
                        Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('文件已存在');</script>");
                    }
                    else
                    {

                        string filename2 = filename.Replace(fileext, "");
                        bool b1 = new FileInfoManager().Insert(name, parentid, filename2, fileext, size);
                        if (b1)
                        {
                            FileUpload1.SaveAs(Server.MapPath("Upfile")  + "\\"  +path +"\\"+ FileUpload1.FileName);
                        }

                    }

                }
            }
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('请选择文件');</script>");
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        string q = Request.QueryString["q"].ToString();
        if (q == "aa")
        {
            Response.Redirect("../Web/Manager.aspx?p=0&q=aa");
            
        }
        else
        {
            string p = Request.QueryString["p"].ToString();
            string dd = Request.QueryString["dd"].ToString();
            Response.Redirect("../Web/Manager.aspx?p="+p+"&dd="+dd+"&q=pp");
        }
    }
}
